
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    citys: [],
    value1: 0, 
    types: [],
    value2: 0,
    areaid:'',//筛选区域
    type:'',//筛选分类
    searchval:'',//搜索

    imageBaseURL: app.imageBaseURL,
    imageResizeParams: app.imageResizeParams,
    list:[],//列表
    page:1,
    pagesize:10,
    searchnull:false,
   
  },
  //label图片 + https
  async loadImg(e){
      let url =  await api.checkurl(e.currentTarget.dataset.url)
      let arrt = e.currentTarget.dataset.arrt
      let ind = e.currentTarget.dataset.index
      this.data.list[ind][arrt] = url;
      this.setData({
        list:this.data.list
      })
  
  },
  //搜索
  onSearch(e){
    this.setData({
      searchval:e.detail,
      list:[],
      page:1,
      pagesize:10,
    })
    this.getNews(this.data.areaid,this.data.type,this.data.searchval)
  },
  onCancel(e){
    this.setData({
      searchval:e.detail,
      list:[],
      page:1,
      pagesize:10,
    })
    this.getNews(this.data.areaid,this.data.type,this.data.searchval)
  },
  //筛选
  select(e){
    // 1-全国 2-分类
    let ind = e.currentTarget.dataset.type;
    if(ind == 1){
      this.setData({
        page:1,
        pagesize:10,
        areaid:e.detail,
        list:[],
      })
    }else if(ind == 2){
      this.setData({
        page:1,
        pagesize:10,
        type:e.detail,
        list:[],
      })
    }
    
    this.getNews(this.data.areaid,this.data.type,this.data.searchval)
  },
  //咨询
  async getNews (areaid,type,searchval) {
    let data={}
    data['flag'] = 'info_query'
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    data['areaid'] = areaid
    data['type'] = type
    data['searchval'] = searchval
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result && result.forEach((item)=>{
        item.headimg = api.checkurl(item.headimg)
        item.imgurl = api.checkurl(item.imgurl)
      })
      if( result && result.length>0 ){
        let page = this.data.page+1
        let str = 'list['+(this.data.page-1)+']'
        this.setData({
          [str]:result,page
        })
      }
      if(this.data.list.length == 0){
        this.setData({
          searchnull:true
        })
      }
    }else {
      if(this.data.list.length == 0){
        this.setData({
          searchnull:true
        })
      }
      // Toast(message)
    }
  },

  //咨询-城市-分类
  async getCityType () {
    let data={}
    data['flag'] = 'info_get_data'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.addr.forEach((item,index) => {
        item.text = item.label
      })
      result.cate.forEach((item,index) => {
        item.text = item.label
      })
      result.addr.unshift({text :"全国",value : 0,})
      result.cate.unshift({text :"分类",value : 0,})
      this.setData({
        citys:result.addr,
        types:result.cate
      })
      
    }else {
      // Toast(message)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    this.getCityType()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page:1,
      pagesize:10,
    })
    // wx.hideTabBar()
    this.getNews(this.data.areaid,this.data.type,this.data.searchval)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNews(this.data.areaid,this.data.type,this.data.searchval)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})